package com.sjzd.meeting.controller;

import com.github.pagehelper.PageInfo;
import com.sjzd.meeting.pojo.MeetingGrab;
import com.sjzd.meeting.pojo.MeetingPublish;
import com.sjzd.meeting.service.IMeetingGrabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/meetingGrab")
public class MeetingGrabController {

    @Autowired
    private IMeetingGrabService meetingGrabService;

    /**
     *  根据要求返回有多少订单可抢
     * @param uid
     * @param model
     * @return
     */
    @GetMapping("/grab")
    public String grab(@RequestParam(name="uid") Integer uid,
                       @RequestParam(name="tid", defaultValue = "0") String tid,
                       @RequestParam(name="pageNum",defaultValue = "1") String pageNum,
                       Model model) {
        PageInfo<MeetingPublish> pageInfo =  meetingGrabService.selectByUidAndTid(uid, Integer.parseInt(tid),
                Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("uid", uid);
        return "weixin/grab";
    }


    /**
     * 处理抢单请求
     * @param meetingGrab
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/grab")
    public String doGrab(MeetingGrab meetingGrab,
                         Model model) {
        meetingGrab.setId(UUID.randomUUID().toString());
        meetingGrab.setCreateTime(new Date());
        meetingGrab.setStatus(true);
        meetingGrab.setMatchStatus(0);
        int flag = meetingGrabService.save(meetingGrab);
        if (flag == 1) {
            return "2";
        }else{
            return "1";
        }
    }

    /**
     *  查询自己已抢了多少个订单
     * @param uid
     * @param model
     * @return
     */
    @GetMapping("/grabed")
    public String grabed(@RequestParam(name="uid") Integer uid,
                       @RequestParam(name="pageNum",defaultValue = "1") String pageNum,
                       Model model) {
        PageInfo<MeetingGrab> pageInfo =  meetingGrabService.selectByUid(uid, Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("uid", uid);
        return "weixin/grabed";
    }

    /**
     *  选定抢单者
     * @param id
     * @param mid
     * @return
     */
    @ResponseBody
    @PostMapping("/confirmCandidate")
    public String confirmCandidate(@RequestParam(name="id") String id, @RequestParam(name="mid") String mid) {
       int flag =  meetingGrabService.confirmCandidate(id, mid);
       return "" + flag;
    }


    @GetMapping("/candidates")
    public String candidates(@RequestParam(name="mid") String mid,
                             @RequestParam(name="pageNum",defaultValue = "1") String pageNum,
                             Model model) {
        PageInfo<MeetingGrab> pageInfo =  meetingGrabService.selectCandidateByMid(mid,Integer.parseInt(pageNum), 3);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("mid", mid);
        return "weixin/candidates";
    }
}
